package com.musiclibrary.common;

import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.nodes.Document;

import com.musiclibrary.scraper.HtmlForm;
import com.musiclibrary.scraper.WebPage;

public class JsoupUtils {

	private String userAgent="Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.63 Safari/537.36";
	
	public <T> Document loadPage(WebPage<T> webPage) {
		Document document = null;
		try {
			if (webPage != null) {
				String url=webPage.getUrl();
				if(url!=null){
					HtmlForm htmlForm = webPage.getHtmlForm();
					Method method= htmlForm.getMethod().equals(Method.POST)?Method.POST:Method.GET;
					Connection connection = Jsoup.connect(url).userAgent(userAgent).method(method).timeout(10000);
					if(webPage.getHtmlForm().getFromParameters()!=null && !webPage.getHtmlForm().getFromParameters().isEmpty()){
						connection.data(webPage.getHtmlForm().getFromParameters());
					}
					
					Response response = connection.execute();
//					System.out.println(response.url());
					document= response.parse();
				}//url null
			}else{
				//web page null
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return document;
	}

}
